<template>
  <div class="home">
    <bread-crumb title="区域管理"></bread-crumb>
    <div class="new_row">
      <el-button type="primary" @click="addShowArea">添加演出地址</el-button>
    </div>
    <el-table :data="datas" border stripe>
      <el-table-column prop="add_id" label="场地编号" width="50px"></el-table-column>
      <el-table-column align="center" prop="area_name" label="场地名" width="250px"></el-table-column>
      <el-table-column align="center" prop="area_img" label="场地图片" width="150px">
        <template slot-scope='scope'>
          <img v-show="scope.row.area_img" :src="scope.row.area_img" width="50px" height="50px" style="border-radius: 50%;">
        </template>
      </el-table-column>
      <el-table-column align="center" prop="area_hot" label="场地热度" width="100px"></el-table-column>
      <el-table-column align="center" prop="area_fans" label="场地粉丝" width="100px"></el-table-column>
      <el-table-column align="center" prop="area_city" label="场地城市" width="<10></10>0px"></el-table-column>
      <el-table-column align="center" prop="area_place" label="场地地点" width="250px"></el-table-column>
      <el-table-column
            fixed="right"
            label="操作"
            width="250">
            <template slot-scope="scope">
              <el-button @click="viewRow(scope.row)" type="primary" size="small">详情</el-button>
              <el-button @click="editRow(scope.row)" type="primary" size="small">编辑</el-button>
              <el-button @click="delRow(scope.row)" type="danger" size="small">删除</el-button>
            </template>
          </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalRows">
     </el-pagination>
     <el-dialog title="删除提醒" :visible.sync="is_show" @close="is_show=false">
       <span class="text-danger">{{ del_msg }}</span>
       <div slot="footer">
         <el-button @click="is_show=false">取消</el-button>
         <el-button @click="delData" type="danger">确定</el-button>
       </div>
     </el-dialog>
     <el-dialog :title="editOrAddTitle" :visible.sync="edit_is_show" @close="editForm = {}">
       <el-form :model="editForm" class="editForm">
         <el-form-item label="场地名">
           <el-input v-model="editForm.area_name"></el-input>
         </el-form-item>
         <el-form-item label="场地图片">
           <el-input v-model="editForm.area_img"></el-input>
         </el-form-item>
         <el-form-item label="场地热度">
           <el-input v-model="editForm.area_hot"></el-input>
         </el-form-item>
         <el-form-item label="场地粉丝">
           <el-input v-model="editForm.area_fans"></el-input>
         </el-form-item>
         <el-form-item label="场地演出">
           <el-input v-model="editForm.area_show"></el-input>
         </el-form-item>
         <el-form-item label="场地详情">
           <el-input type="textarea" v-model="editForm.area_details"></el-input>
         </el-form-item>
         <el-form-item label="场地地址">
           <el-input v-model="editForm.area_address"></el-input>
         </el-form-item>
         <el-form-item label="场地城市">
           <el-input v-model="editForm.area_city"></el-input>
         </el-form-item>
         <el-form-item label="场地地点">
           <el-input v-model="editForm.area_place"></el-input>
         </el-form-item>
       </el-form>
       <div slot="footer">
         <el-button @click="edit_is_show=false">取消</el-button>
         <el-button @click="saveForm" type="primary">确定</el-button>
       </div>
     </el-dialog>
     <el-dialog title="场地详情" :visible.sync="view_is_show" @close="view_is_show=false">
       <template>
         <el-tabs :tab-position="tabPosition" style="height: 600px">
           <el-tab-pane label="场地信息">
             <span>场地id： {{ viewData.add_id }}</span>
             <el-divider></el-divider>
             <span>场地名： {{ viewData.area_name }}</span>
             <el-divider></el-divider>
             <span>场地图片：
              <el-image
                 style="width: 100px; height: 100px"
                 :src="viewData.area_img"
                 :preview-src-list="srcList">
               </el-image>
             </span>
             <el-divider></el-divider>
             <span>场地热度： {{ viewData.area_hot }}</span>
             <el-divider></el-divider>
             <span>粉丝： {{ viewData.area_fans }}</span>
             <el-divider></el-divider>
             <span>城市： {{ viewData.area_city }}</span>
             <el-divider></el-divider>
             <span>地点： {{ viewData.area_place }}</span>
             <el-divider></el-divider>
             <span>演出： {{ viewData.area_show }}</span>
             <el-divider></el-divider>
           </el-tab-pane>
           <el-tab-pane label="场地介绍">
             <p>&nbsp;&nbsp; {{ viewData.area_details }}</p>
           </el-tab-pane>
         </el-tabs>
       </template>
     </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      datas: [],
      currentPage: 1,
      pageSize: 10,
      totalRows: 0,
      is_show: false,
      view_is_show: false,
      del_msg: '是否删除',
      del_item: {},
      editOrAddTitle: '修改数据',
      edit_is_show: false,
      editForm: {},
      editData: {},
      viewData: {},
      disabled: false,
      tabPosition: 'left',
      srcList: []
    }
  },
  methods: {
    loadData () {
      this.$http.get('/adminapi/area/?page=' + this.currentPage + '&page_size=' + this.pageSize).then(resp => {
        const { results, count } = resp.data
        this.datas = results
        this.totalRows = count
      })
    },
    addShowArea () {
      this.editForm.title = '新增演出场地'
      this.edit_is_show = true
      this.disabled = false
    },
    viewRow (item) {
      this.viewData = item
      this.view_is_show = true
      this.srcList = [item.area_img]
    },
    editRow (editData) {
      this.edit_is_show = true
      this.disabled = true
      // 初始显示的数据
      this.editForm = {
        area_name: editData.area_name,
        add_id: editData.add_id,
        area_img: editData.area_img,
        area_hot: editData.area_hot,
        area_fans: editData.area_fans,
        area_show: editData.area_show,
        area_details: editData.area_details,
        area_address: editData.area_address,
        area_city: editData.area_city,
        area_place: editData.area_place
      }
    },
    saveForm () {
      if (this.disabled) {
        const { add_id } = this.editForm
        // 删除对象的属性(不修改的)
        delete this.editForm.add_id
        this.$http.patch('/adminapi/area/' + add_id + '/', this.editForm).then(resp => {
          console.info(resp.data)
          this.edit_is_show = false
          this.loadData()
          this.editForm = {} // 清空之前绑定的数据
        })
      } else {
        this.$http.post('/adminapi/area/', this.editForm).then(resp => {
          console.info(resp.data)
          this.edit_is_show = false
          this.loadData()
        })
      }
    },
    delRow (item) {
      this.del_item = item
      this.is_show = true
      this.del_msg = '亲，真的忍心删除' + item.area_name + '吗？'
    },
    delData () {
      this.$http.delete('/adminapi/area/' + this.del_item.add_id + '/').then(resp => {
        console.info(resp.data)
        this.loadData() // 刷新页面数据，重新装载数据
        this.$message.success('数据删除成功')
        this.is_show = false
      })
    },
    handleSizeChange (size) {
      console.info(size)
      this.currentPage = 1
      this.pageSize = size
      this.loadData()
    },
    handleCurrentChange (page) {
      console.info(page)
      this.currentPage = page
      this.loadData()
    },
    handleClick (tab, event) {
      console.log(tab, event)
    }
  },
  mounted () {
    this.loadData()
  }
}
</script>

<style>
.editForm{
  width: auto;
  height: auto;
}
</style>
